using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using Biblioteca.Objetos;
using SEG.Utils;
using Biblioteca.Utils;
using SEG.Objetos;
using Pms.Utils;

namespace SEG
{
   public partial class ConsultaLog : System.Web.UI.Page
   {
      Usuario usuAut;

      protected void Page_Load(object sender, EventArgs e)
      {
         if (Session.Count == 0)
         {
            Response.Redirect("~/Login.aspx");
         }
         else
         {
            usuAut = (Usuario)Session["usuario"];
            char perm = BiblioPMS.VerifiquePermissao("CONSULTALOG.ASPX", BiblioPMS.aplicacaoSEG, usuAut.Login);
            if (perm.Equals('N'))
            {
               Response.Redirect("Inicio.aspx");
            }
         }
      }

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (Session == null)
          {
              Response.Redirect("~/Login.aspx");
          }
          else
          {
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  e.Row.Cells[2].Text = BiblioSEG.obterOperacao(e.Row.Cells[2].Text[0]);

                  string chave = ((DataRowView)e.Row.DataItem).Row["chave"].ToString();
                  string log = ((DataRowView)e.Row.DataItem).Row["log"].ToString();
                  Table table = (Table)e.Row.Cells[4].Controls[1];
                  TableRow tblChave = new TableRow();
                  TableRow tblChave2 = new TableRow();
                  TableCell tblCell = new TableCell();
                  tblCell.Font.Bold = true;
                  tblCell.BackColor = System.Drawing.Color.Gainsboro;

                  TableCell tblCell2 = new TableCell();

                  tblCell.Text = "Chave"; ;
                  tblCell2.Text = chave;

                  tblChave.Cells.Add(tblCell);
                  tblChave2.Cells.Add(tblCell2);

                  table.Rows.Add(tblChave);
                  table.Rows.Add(tblChave2);
                  XmlDocument doc = new XmlDocument();
                  doc.LoadXml(log);

                  XmlNodeList nl = doc.GetElementsByTagName("log");
                  XmlElement ele = (XmlElement)nl.Item(0);
                  XmlNodeList nl1 = ele.ChildNodes;
                  for (int i = 0; i < nl1.Count; i++)
                  {
                      XmlElement element = (XmlElement)nl1.Item(i);
                      TableRow row = new TableRow();
                      TableCell cell = new TableCell();
                      cell.Text = element.Name;
                      cell.Font.Bold = true;
                      cell.BackColor = System.Drawing.Color.Gainsboro;
                      cell.Width = Unit.Pixel(280);
                      row.Cells.Add(cell);

                      TableRow row2 = new TableRow();
                      TableCell cell2 = new TableCell();
                      XmlText text = (XmlText)element.ChildNodes.Item(0);
                      cell2.Text = text.Data;
                      cell2.Width = Unit.Pixel(280);
                      row2.Cells.Add(cell2);

                      table.Rows.Add(row);
                      table.Rows.Add(row2);
                  }
              }
          }
      }

      protected void Button1_Click(object sender, EventArgs e)
      {
          try
          {
              if (txtChave.Text.Length == 0)
                  SqlDataSource1.SelectParameters["chave"].DefaultValue = "#TODAS#";

              if (dtInicio.SelectedDate.ToShortDateString().Length == 0)
                  SqlDataSource1.SelectParameters["datini"].DefaultValue = "01/01/1900";
              else
                  SqlDataSource1.SelectParameters["datini"].DefaultValue = dtInicio.SelectedDate.ToShortDateString();

              if (dtFim.SelectedDate.ToShortDateString().Length == 0)
                  SqlDataSource1.SelectParameters["datfim"].DefaultValue = "31/12/2099";
              else
                  SqlDataSource1.SelectParameters["datfim"].DefaultValue = dtFim.SelectedDate.ToShortDateString();

              GridView1.Visible = true;
              GridView1.DataBind();
          }
          catch (Exception ex)
          {
              lblErro.Text = "*Erro: " + ex.Message;
              lblErro.Visible = true;
              GridView1.Visible = false;
          }
      }

      protected void GridView1_PageIndexChanged(object sender, EventArgs e)
      {

      }
   }
}
